/*
 * @Author: lei
 * @Date: 2021-08-03 15:50:45
 * @LastEditTime: 2021-08-03 16:29:59
 * @LastEditors: Please set LastEditors
 * @Description: vector的push_back()与emplace_back()区别，零拷贝技术?
 * @FilePath: /code/emplace_back.cpp
 */

#include <vector>
#include <string>
#include <iostream>
using namespace std;

struct Person
{
    string name;
    int age;
    //初始构造函数
    Person(string p_name, int p_age): name(std::move(p_name)), age(p_age)
    {
         cout << "I have been constructed" <<endl;
    }
     //拷贝构造函数
     Person(const Person& other): name(std::move(other.name)), age(other.age)
    {
         cout << "I have been copy constructed" <<endl;
    }
     //移动构造函数
     Person(Person&& other): name(std::move(other.name)), age(other.age)
    {
         cout << "I have been moved"<<endl;
    }
    ~Person()
    {
        cout<<"I am destructed!"<<endl;
    }
};

int main()
{
    {
        vector<Person> e;
        cout << "emplace_back:" <<endl;
        e.emplace_back("Jane", 23); //一次默认构造
    }
    {
        vector<Person> p;
        cout << "push_back:"<<endl;
        p.push_back(Person("Mike",36));//一次默认构造+一次移动构造
    }
    return 0;

}
//输出结果：
/*
emplace_back:
I have been constructed
I am destructed!
push_back:
I have been constructed
I have been moved
I am destructed!
I am destructed!
*/

